@extends('admin::layouts.master')

@section('title')用户管理@endsection

@section('css')

@endsection

@section('nav')
    <div class="row mb-2">
        <div class="col-sm-6">
            <h1 class="m-0">配置管理</h1>
        </div>
        <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="{{route('admin.configuration')}}">配置管理</a></li>
                <li class="breadcrumb-item active">修改配置</li>
            </ol>
        </div>
    </div>

@endsection

@section('content')
    <div class="card">
        <div class="card-header">
            <a class="btn btn-success" href="{{route('admin.configuration.create')}}">新建配置</a>
        </div>
        <div class="card-body">
            @if(count($groups))
                <ul class="nav nav-tabs" id="custom-content-below-tab" role="tablist">
                    @foreach($groups as $key => $group)
                        <li class="nav-item">
                            <a class="nav-link {{$key == 0 ?'active':''}}" id="custom-content-below-{{$group->id}}-tab"
                               data-toggle="pill" href="#custom-content-below-{{$group->id}}" role="tab"
                               aria-controls="custom-content-below-{{$group->id}}" aria-selected="false">
                                {{$group->name}}
                            </a>
                        </li>
                    @endforeach
                </ul>
                <div class="tab-content" id="custom-content-below-tabContent" style="margin-top: 40px;">
                    @foreach($groups as $key => $subGroup)
                        <div class="tab-pane fade {{$key==0 ? 'show active':''}}"
                             id="custom-content-below-{{$subGroup->id}}" role="tabpanel"
                             aria-labelledby="custom-content-below-{{$subGroup->id}}-tab">
                            <form class="form-horizontal" name="form" action="##">
                                {{csrf_field()}}
                                {{method_field('put')}}
                                <input type="hidden" name="group_id" value="{{$subGroup->id}}">
                                @if(count($subGroup->configurations))
                                    @foreach($subGroup->configurations as $config)
                                        @switch($config->type)
                                            @case('input')
                                            <div class="form-group row">
                                                <label for="phone"
                                                       class="col-sm-2 col-form-label">{{$config->label}}</label>
                                                <div class="col-sm-5">
                                                    <input type="text" class="form-control" id="{{$config->key}}"
                                                           name="{{$config->key}}"
                                                           value="{{$config->val??old($config->key)}}">
                                                </div>
                                            </div>
                                            @break
                                            @case('textarea')
                                            <div class="form-group row">
                                                <label for="phone"
                                                       class="col-sm-2 col-form-label">{{$config->label}}</label>
                                                <div class="col-sm-5">
                                                        <textarea type="text" class="form-control" id="{{$config->key}}"
                                                                  name="{{$config->key}}">{{$config->val??old($config->key)}}</textarea>
                                                </div>
                                            </div>
                                            @break
                                            @case('select')

                                            @break
                                            @case('radio')
                                            <div class="form-group row">
                                                <label class="col-sm-2 col-form-label">{{$config->label}}</label>
                                                <div class="col-sm-5">
                                                    @foreach(explode('|',$config->content) as $content)
                                                        <label class="radio-inline col-sm-3">
                                                            <input type="radio" class="custom-radio"
                                                                   name="{{$config->key}}"
                                                                   value="{{explode(':',$content)[0]}}"
                                                                   checked>{{explode(':',$content)[1]}}
                                                        </label>
                                                    @endforeach
                                                </div>
                                            </div>
                                            @break
                                        @endswitch
                                    @endforeach
                                @endif
                                <div class="col-sm-7" style="text-align: center;">
                                    <a  class="save btn btn-dark">保 存</a>
                                </div>
                            </form>
                        </div>
                    @endforeach
                </div>
            @endif
        </div>
    </div>
@endsection

@section('js')
    <script>
        $('.save').click(function(){
            $.ajax({
                type: "put",//方法类型
                dataType: "json",//预期服务器返回的数据类型
                url: "{{route('admin.configuration.update')}}" ,//url
                data: $(this).parents('.form-horizontal').serialize(),
                success: function (result) {
                    if (result.code == 0) {
                        toastr.success(result.msg);
                    }else{
                        toastr.error(result.msg);
                    }
                },
                error : function() {
                    toastr.error('网络错误');
                }
            });
        })
    </script>
@endsection